package ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.MessageAdapter;
import adapter.NoticeAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.LoginBean;
import bean.MessageBean;
import bean.NoticeBean;
import student.andlei.com.student.R;
import utils.GsonUtils;

public class MessageActivity extends BaseNetActivity {
    private RecyclerView recyclerView;
    private TextView tv_title,
            tv_content;
    private MessageAdapter adapter;
    private NoticeAdapter notice_adapter;
    private List<MessageBean.Data> list = new ArrayList<>();
    private List<NoticeBean> noticeBeanList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

       // loaddata();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        hideLoadDialog();
        if (baseBean.code == 200) {
            if(action.equals("personal/getMsg")){
                list = GsonUtils.getInstance(this).parseNoHeaderJArray(baseBean, MessageBean.Data.class);
                if (list != null && list.size() > 0) {
                    adapter.setNewData(list);
                    list.get(0).setIsClick(1);
                    tv_content.setText(list.get(0).content);
                    tv_title.setText(list.get(0).title);
                }
            }else if(action.equals("common/getNoticeList")){
                noticeBeanList = GsonUtils.getInstance(context).parseNoHeaderJArray(baseBean,NoticeBean.class);
                //创建布局管理
                LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                notice_adapter = new NoticeAdapter(R.layout.item_message, noticeBeanList);
                recyclerView.setAdapter(notice_adapter);
                notice_adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        updateUi(noticeBeanList.get(position));
                    }
                });
            }else if(action.equals("common/getNotice")){
                NoticeBean noticeBean = (NoticeBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,NoticeBean.class);
                updateUi(noticeBean);
            }

        }else {
            if(action.equals("personal/editMsg")){
                return;
            }
           toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {
        hideLoadDialog();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void findViews() {
        recyclerView = getView(R.id.rcly_);
        tv_title = getView(R.id.tv_title);
        tv_content = getView(R.id.tv_content);
    }

    @Override
    protected void formatViews() {

        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new MessageAdapter(this, R.layout.item_message, list);
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEmptyView(R.layout.empty_view);
        ((TextView)(adapter.getEmptyView().findViewById(R.id.tv_empty))).setText("你还没有消息哦");
//        if(getTitle().equals("消息")){
//            ((TextView)(adapter.getEmptyView().findViewById(R.id.tv_empty))).setText("你还没有消息哦");
//        }else {
//            ((TextView)(adapter.getEmptyView().findViewById(R.id.tv_empty))).setText("你还没有公告哦");
//        }

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < list.size(); i++) {
                    if (i == position) {
                        list.get(i).setIsClick(1);
                        list.get(i).status=1;
                        Map<String,String> map = new HashMap<>();
                        map.put("message_id",list.get(i).message_id+"");
                        post("personal/editMsg",map,BaseBean.class,true);
                    } else {
                        list.get(i).setIsClick(0);
                    }
                }
                adapter.notifyDataSetChanged();
                tv_content.setText(list.get(position).content);
                tv_title.setText(list.get(position).title);
            }
        });
    }

    @Override
    protected void formatData() {

//        for(int i=0;i<10;i++){
//            MessageBean bean = new MessageBean();
//            bean.setIsClick(0);
//            bean.setContent("hahhahahahhahahahahhahahah"+i);
//            bean.setIsRead(0);
//            bean.setTitle("标题"+i);
//            bean.setDateString("2018-8-"+i);
//            list.add(bean);
//        }
//        list.get(0).setIsClick(1);
//        list.get(0).setIsRead(1);
//        tv_content.setText(list.get(0).getContent());
//        tv_title.setText(list.get(0).getTitle());
    }

    private void loaddata() {
        Map<String, String> map = new HashMap<>();
        post("personal/getMsg", map, LoginBean.class, false);

    }
    private void loadnoticedata(int id) {
        Map<String,String> map = new HashMap<>();
        map.put("id",id+"");
        post("common/getNotice",map,BaseBean.class,false);
        post("common/getNoticeList", BaseBean.class, false);

    }
    private void  updateUi(NoticeBean bean){
        tv_title.setText(bean.title);
        tv_content.setText(bean.content);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        if(bundle == null){
            setTitle("消息");
            loaddata();
        }else {
            setTitle("公告");
            loadnoticedata(bundle.getInt("noticeid"));
        }
    }

    @Override
    public void onClick(View view) {

    }
}
